package com.cskaoyan.market.controller.Admin;/*
package com.cskaoyan.market.controller;*/
/*
 *文件名: GeneralProblemController
 *创建者:wxin
 *创建时间:2024/5/6 14:52
 */


import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.service.admin.AdminIssueService;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/issue")
public class AdminIssueController {
    @Autowired
    AdminIssueService issueService;
    @Autowired
    ObjectMapper objectMapper;

    @PostMapping("delete")
    public Object delete(@RequestBody Map map) throws IOException {
        Integer id = (Integer) map.get("id");
        if (id == null) {
            Object fail = ResponseUtil.fail();
            return fail;
        }
        int result = issueService.logicalDelete(id);
        if (result == 0) {
            Object fail = ResponseUtil.fail();
            return fail;
        }
        return ResponseUtil.ok();
    }

    @PostMapping("update")
    public Object update(@RequestBody MarketIssue marketIssue) throws IOException {
        String question = marketIssue.getQuestion();
        String answer = marketIssue.getAnswer();
        //String requestBody = JacksonUtil.getObjectMapper().writeValueAsString(map);
        if (StringUtils.isEmpty(question)) {
            Object fail = ResponseUtil.fail(404, "问题不能为空");
            return fail;
        }
        if (StringUtils.isEmpty(answer)) {
            Object fail = ResponseUtil.fail(404, "问题不能为空");
            return fail;
        }

        MarketIssue resultmarketIssue = issueService.updateIssue(marketIssue);
        Map<String, Object> data = new HashMap<>();
        data.put("id", resultmarketIssue.getId());
        data.put("question", resultmarketIssue.getQuestion());
        data.put("answer", resultmarketIssue.getAnswer());
        data.put("addTime", resultmarketIssue.getAddTime());
        data.put("updateTime", resultmarketIssue.getUpdateTime());
        data.put("deleted", resultmarketIssue.getDeleted());
        return ResponseUtil.ok();
    }

    @PostMapping("create")
    public Object create(@RequestBody MarketIssue marketIssue) throws IOException {
        String question = marketIssue.getQuestion();
        String answer = marketIssue.getAnswer();
        if (StringUtils.isEmpty(question)) {
            Object object = ResponseUtil.badArgument();
            return object;
        }
        if (StringUtils.isEmpty(answer)) {
            Object object = ResponseUtil.badArgument();
            return object;
        }
        //Jackson工具类，将json字符串转java对象
        MarketIssue resultmarketIssue = issueService.addIssue(marketIssue);
        HashMap<String, Object> data = new HashMap<>();
        data.put("addTime", resultmarketIssue.getAddTime());
        data.put("answer", resultmarketIssue.getAnswer());
        data.put("id", resultmarketIssue.getId());
        data.put("question", resultmarketIssue.getQuestion());
        data.put("updateTime", resultmarketIssue.getUpdateTime());
        return ResponseUtil.ok(data);
    }

    @GetMapping("list")
    public Object list(String page, String limit, String sort, String order, String question) throws IOException {
        Integer pageParam = null;
        Integer limitParam = null;
        try {
            pageParam = Integer.parseInt(page);
            limitParam = Integer.parseInt(limit);

        } catch (NumberFormatException e) {
            Object object = ResponseUtil.badArgument();
            return object;
        }
        List<MarketIssue> adminIssueList = issueService.list(limitParam, pageParam, question, sort, order);
        return ResponseUtil.okList(adminIssueList);
    }
}
